package com.aeasycredit.uploaddatas;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import com.rainhy.core.exception.RainhyException;

public class UploadDatas {
    
    private CloseableHttpClient client = HttpClients.createDefault();
    
    @Test
    public void uploadDatas() {
        login();
        
    }
    
    private void login() {
        Map<String, String> header = new HashMap<>();
        header.put("X-Requested-With", "XMLHttpRequest");
        header.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        header.put("Origin", "http://113.200.27.104");
        header.put("Referer", "http://113.200.27.104/SmartReport/pettyloan.jsp");
        String sendPostMethod = sendPost("http://113.200.27.104/SmartReport/system/login/normalLogin.do", "userCode=30433101&userPwd=aeasycredit2015&userOrg=304331", header);
        System.out.println("1--normalLogin: " + sendPostMethod);
        
        header.clear();
        
        header.put("Referer", "http://113.200.27.104/SmartReport/pettyloan.jsp");
        header.put("Content-Type", "application/x-www-form-urlencoded");
        header.put("Origin", "http://113.200.27.104");
        String sendPost = sendPost("http://113.200.27.104/SmartReport/system/login/loginSuccess.do", "userCode=30433101&userOrg=304331&hostName=undefined&macAddr=undefined&ipAddr=undefined", header);
        System.out.println("2--loginSuccess: " + sendPost);
    }
    
    private String sendPost(String url, String post, Map<String, String> header) {
        CloseableHttpResponse response = null;
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new StringEntity(post, "UTF-8"));
            if (MapUtils.isNotEmpty(header)) {
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    httpPost.setHeader(entry.getKey(), entry.getValue());
                }
            }
            response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            throw new RainhyException(e, "http GET error");
        } finally {
            IOUtils.closeQuietly(response);
            //            IOUtils.closeQuietly(client);
        }
    }
    
    //    private JsonObject sendPostMethodAndReturnJsonObjectByWeChat(String url, String post, Map<String, String> header) {
    //        JsonParser jsonParser = new JsonParser();
    //        String responseContent = sendPost(url, post, header);
    //        JsonObject json = jsonParser.parse(responseContent).getAsJsonObject();
    //        if (json.get("errcode") != null) {
    //            throw new RainhyException("posturl异常   ----   " + url + "  ---   " + json.toString());
    //        }
    //        return json;
    //    }
}
